#include "common.h"
void show_time()
{
    time_t rawtime;
    struct tm *timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    // show_num(600, 30, timeinfo->tm_year);
    // show_num(680, 30, timeinfo->tm_mon);
    // show_num(720, 30, timeinfo->tm_day);

    show_num(800, 30, timeinfo->tm_hour + 9);
    show_word(transact(850), transact(30), 32, 29, maohao, 0x000000);
    show_num(890, 30, timeinfo->tm_min + 12);
}
void to_zhuye()
{
    show_bmp(0, 0, "./photos/suoping.bmp");
    while (1)
    {
        if (swipe_unlock() == 1)
        {
            printf("success\n");
            change_screen(XIANGQING);
            break;
        }
        else
        {
            printf("fail\n");
        }
    }
}
void to_xiangqing()
{
    pthread_t tid;
    Data data;
    // show_a_color(0xfxxxxxxxxxxxxxxxxxxxfffff);
    pthread_create(&tid, NULL, get_gy39, &data);
    //sleep(1);

    show_bmp(0, 0, "./photos/zhuye.bmp");
    show_float(45, 503, data.tem);
    show_float(240, 503, data.lux);
    show_float(390, 503, data.press);
    show_float(580, 503, data.humid);
    show_float(750, 503, data.height);
    show_time();

    while (1)
    {
        pair p = test_touch();
        printf("x = %d, y = %d", p.x, p.y);
        if (p.x >= 900 && p.x <= 1100 && p.y >= 500 && p.y <= 600)
        {
            // show_a_color(0xffffff);
            show_bmp(0, 0, "./photos/zhuye.bmp");
            show_float(45, 503, data.tem);
            show_float(240, 503, data.lux);
            show_float(390, 503, data.press);
            show_float(580, 503, data.humid);
            show_float(750, 503, data.height);
            show_time();
            printf("refresh");
        }
        else if (p.x >= 120 && p.x <= 480 && p.y >= 150 && p.y <= 300)
        {
            printf("light");
            change_screen(DENGGUANG);
            break;
        }
        else if (p.x >= 520 && p.x <= 900 && p.y >= 150 && p.y <= 300)
        {
            printf("sound");
            change_screen(YINYUE);
            break;
        }
        else if (p.x >= 0 && p.x <= 200 && p.y >= 0 && p.y <= 100)
        {
            change_screen(ZHUYE);
            break;
        }
    }
}
void to_dengguang()
{
    show_bmp(0, 0, "./photos/dengguang.bmp");
    int led1 = 0, led2 = 0, led3 = 0, led4 = 0;
    while (1)
    {
        pair p = test_touch();
        printf("x = %d, y = %d", p.x, p.y);
        if (p.x >= 0 && p.x <= 200 && p.y >= 0 && p.y <= 100)
        {
            change_screen(XIANGQING);
            break;
        }
        else if (p.x >= 100 && p.x <= 320 && p.y >= 150 && p.y <= 300) // light1
        {
            printf("b1");
            led1 = led1 ? 0 : 1;
            printf("led1 = %d", led1);
            bled_ctrl(LED1, led1);
        }
        else if (p.x >= 450 && p.x <= 700 && p.y >= 150 && p.y <= 300) // light2
        {
            printf("b2");
            bled_ctrl(LED2, !led2);
            led2 = !led2;
        }
        else if (p.x >= 300 && p.x <= 500 && p.y >= 330 && p.y <= 460) // light3
        {
            printf("b3");
            bled_ctrl(LED3, !led3);
            led3 = !led3;
        }
        else if (p.x >= 630 && p.x <= 830 && p.y >= 330 && p.y <= 460) // light3
        {
            printf("b4");
            bled_ctrl(LED4, !led4);
            led4 = !led4;
        }
    }
}
void to_yinyue()
{
    show_bmp(0, 0, "./photos/bofang.bmp");
    int ing = 0;
    while (1)
    {
        pair p = test_touch();
        printf("x = %d, y = %d", p.x, p.y);
        if (p.x >= 0 && p.x <= 200 && p.y >= 0 && p.y <= 100)
        {
            music(5);
            change_screen(XIANGQING);
            break;
        }
        if (p.x >= 350 && p.x <= 400 && p.y >= 500 && p.y <= 550) // 上
        {
            show_bmp(0, 0, "./photos/zanting.bmp");
            music(2);
            ing = 1;
            // break;
        }
        if (p.x >= 450 && p.x <= 540 && p.y >= 500 && p.y <= 550) // 播放/听
        {
            if (ing == 1)
            {
                show_bmp(0, 0, "./photos/bofang.bmp");
                ing = 0;
                music(3);
            }
            else
            {
                show_bmp(0, 0, "./photos/zanting.bmp");
                ing = 1;
                music(4);
            }
            // break;
        }
        if (p.x >= 600 && p.x <= 650 && p.y >= 500 && p.y <= 550) // 下
        {
            show_bmp(0, 0, "./photos/zanting.bmp");
            music(1);
            ing = 1;
            // break;
        }
    }
}

void change_screen(int index)
{

    switch (index)
    {
    case ZHUYE:
        to_zhuye();
        break;
    case XIANGQING:

        to_xiangqing();
        break;
    case YINYUE:
        to_yinyue();
        break;
    case DENGGUANG:
        to_dengguang();
        break;
    default:
        break;
    }
}
